const fs = require('fs');

var rds = fs.createReadStream('input.txt');
var wrs = fs.createWriteStream('data.txt');

var globalData = '';
var cnt = 0;


rds.on('data', function (trunk) {
    cnt++;
    console.log('第' + cnt + '次读取，共' + Buffer.byteLength(trunk) + '字节。');
    globalData += trunk;
    //console.log(trunk);
   // wrs.write(globalData);在这种情况下会重复读取文件，改为下面语句
    wrs.write(trunk);
});

rds.on('end', function () {
    console.log("读取文件已完成!");
   // wrs.write(globalData);改读写重复第二种方式，弊端不适用文件内存大
});

rds.on('close', function () {
    console.log("读文件已关闭!");
    wrs.end();
});

rds.on('open', function () {
    console.log("读文件已打开!");
    console.log(globalData);
});

rds.on('error', function (err) {
    console.log(err);
});






wrs.on('finish', function () {
    console.log('写入完成！');
});

wrs.on('error', function (err) {
    console.log(err);
});

wrs.on('close', function () {
    console.log('写文件已关闭！');
});
